import {createPinia, defineStore} from 'pinia'
// 数据持久化
import piniaPluginPersist from 'pinia-plugin-persist'

const pinia = createPinia()
pinia.use(piniaPluginPersist)
export default pinia

class Hooks {
	useStore = defineStore('main', {
		state: (): any => ({
			/** 导航菜单 */
			menus: [],
			/** 所有权限 */
			permissions: [],
			/**
			 * 访问token
			 */
			accessToken: null,
			refreshToken: null,
			/**
			 * 是否显示锁屏对话框
			 */
			lockVisible: false,
			/**
			 * 搜索容器是否渲染完成可见
			 */
			searchVisible: true,
			isFormWx: false,
			/**
			 * 当前登录用户
			 */
			currentUser: {},
			// 钱包用户信息
			walletMemberId: null,
			walletMemberInfo: {}
		}),
		actions: {
			hasPermissions(value: any) {
				if (value) {
					const values = [value].flat()
					return this.permissions.some((permission: any) => {
						return values.includes(permission)
					})
				} else {
					return true
				}
			}
		},
		// 数据持久化设置
		persist: {
			enabled: true,
			strategies: [
				// localStorage存储
				{key: 'YUXIN_PMS_STATE', storage: localStorage}
			]
		}
	})

	setStore(store: any) {
		if (store) {
			this.useStore = store
		}
	}
}

export const storeHooks = new Hooks()

export const useStore = () => storeHooks.useStore()
